home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 203_01 / yam2.c < prev    next >
Text File  |  1980-01-01  |  17KB  |  732 lines

  1. /*
  2. $title ('yam2.c: file transmission protocol handlers')
  3. $date (4 nov 85)
  4. */
  5. /*
  6.  * Ward Christensen Protocol handler for sending and receiving
  7.  * ascii and binary files.  Modified for choice of checksum or crc.
  8.  */
  9.  
  10. #include "yam.h"
  11. #define WCEOT (-10)
  12.  
  13. #ifdef DEFBYTL
  14. long Modtime;        /* Unix style mod time for incoming file */
  15. int Filemode;        /* Unix style mode for incoming file */
  16. #endif
  17.  
  18. /****************************************************************************
  19. FUNCTION:
  20.     Ward Christensen modem 7 protocol file tranmission handler
  21.  
  22. CALLING PARAMETERS:
  23.     argc:
  24.         count of command line arguments.
  25.     argp:
  26.         pointer to string of command line arguments.
  27. ===========================================================================*/
  28. wcsend(argc, argp)
  29. char **argp;
  30. {
  31.     int wcs();
  32.  
  33. #ifdef IBMPC
  34.         /* enbable raw mode */
  35.     enblcm(FALSE);
  36. #endif
  37.  
  38.     Crcflg=FALSE;
  39.     firstsec=TRUE;
  40.  
  41.     if (Batch)
  42.     {
  43.         printf("Sending in Batch Mode\n");
  44.  
  45.             /* send files, batch mode */
  46.         if (expand(wcs, argc, argp)==ERROR)
  47.             goto fubar;
  48.             /* transmit null path name for end of data */
  49.         if (wctxpn("")==ERROR)
  50.             goto fubar;
  51.     }
  52.         /* send file, not batch mode */
  53.     else
  54.     {
  55.         for (; --argc>=0;)
  56.         {
  57.                 /* open file to transmit. open will compute file size */
  58.             if (opentx(1,NULL,*argp++)==ERROR)
  59.                 goto fubar;
  60.                 /* Ward Christensen transmit */
  61.             if (wctx()==ERROR)
  62.                 goto fubar;
  63.         }
  64.     }
  65.     return OK;
  66.  
  67.     /* error, close files and send CAN to other end */
  68. fubar:
  69.     closetx(TRUE);
  70.     canit();
  71.     return ERROR;
  72. } /* wcsend */
  73.  
  74.  
  75. /****************************************************************************
  76. FUNCTION:
  77.     Ward Christensen modem 7 protocol file transmission handler, batch
  78.     mode.
  79.  
  80. CALLING PARAMETERS:
  81.     f_buf:
  82.         pointer to a structure containing information about file
  83.     *pathname:
  84.         pointer to directory path name
  85. ===========================================================================*/
  86. wcs(f_buf,pathname)
  87. struct find_buf *f_buf;
  88. char *pathname;
  89. {
  90.  
  91.         /* open file, open uses f_buf for file size */
  92.     if (opentx(2,f_buf,pathname)==ERROR)
  93.         return OK;        /* skip over inaccessible files */
  94.     if (wctxpn(f_buf->pname)== ERROR)
  95.         return ERROR;
  96.     if (wctx()==ERROR)
  97.         return ERROR;
  98.     printf("\n");
  99.     return OK;
  100. } /* wcs */
  101.  
  102.  
  103. /****************************************************************************
  104. FUNCTION:
  105.     Ward Christensen modem 7 protocol file reception handler
  106.  
  107. CALLING PARAMETERS:
  108.     argc:
  109.         count of command line arguments.
  110.     argp:
  111.         pointer to string of command line arguments.
  112. ===========================================================================*/
  113. wcreceive(argc, argp)
  114. char **argp;
  115. {
  116.     int baslen;
  117.  
  118. #ifdef IBMPC
  119.         /* enbable raw mode */
  120.     enblcm(FALSE);
  121. #endif
  122.  
  123.     if (Batch)
  124.     {
  125.         printf("Receiving in Batch Mode\n");
  126.         for (;;)
  127.         {
  128.                 /* create the basename of pathname if one was specified */
  129.             if (argc > 0)
  130.             {
  131.                 strcpy(Utility.ubuf,*argp);
  132.                 baslen=strlen(Utility.ubuf);
  133.             }
  134.             else
  135.                 baslen = 0;
  136.  
  137.                 /* get file name for batch mode */
  138.             if (wcrxpn(&Utility.ubuf[baslen])== ERROR)
  139.                 goto fubar;
  140.             if (Utility.ubuf[baslen]==0)
  141.                 return OK;
  142.  
  143. #ifdef DEFBYTL
  144.             procheader(Utility.ubuf);
  145. #endif
  146.                 /* receive file */
  147.             if (wcrx(Utility.ubuf)==ERROR)
  148.                 goto fubar;
  149.         }
  150.     }
  151.     else
  152.     for (; --argc>=0;)
  153.     {
  154. #ifdef DEFBYTL
  155.         procheader(0);
  156. #endif
  157. #ifdef XMODEM
  158.         printf("Receive:'%s' FILE OPEN\n", *argp);
  159. #endif
  160.         if (wcrx(*argp++)==ERROR)
  161.         goto fubar;
  162.     }
  163.     return OK;
  164. fubar:
  165.     canit();
  166.     closerx(TRUE);
  167.     return ERROR;
  168. } /* wcreceive */
  169.  
  170.  
  171. /****************************************************************************
  172. FUNCTION:
  173.     Fetch a pathname from the other end as a C ctyle ASCII string.
  174.     Length is indeterminate as long as less than blklen
  175.     a null string represents no more files
  176.  
  177. CALLING PARAMETERS:
  178. ===========================================================================*/
  179. wcrxpn(rpn)
  180. char *rpn;    /* receive a pathname */
  181. {
  182.     purgeline();
  183.     firstsec=TRUE;
  184. #ifdef STATLINE
  185.     lpstat("Fetching pathname");
  186. #else
  187.     printf("\nFetching pathname ");
  188. #endif
  189.         /* slight pause to allow slow systems to get ready */
  190.     sleep(5);
  191.     totsecs = -1;
  192.     if (wcgetsec(rpn, 100, Crcflg?WANTCRC:NAK) != 0)
  193.         return ERROR;
  194.     sendbyte(ACK);
  195.         /* clear line of status message above */
  196. #ifndef STATLINE
  197.     printf("\r                   \r");
  198. #endif
  199.     return OK;
  200. } /* wcrxpn */
  201.  
  202.  
  203. /****************************************************************************
  204. FUNCTION:
  205.     transmit path name for batch mode transmission.
  206.  
  207. CALLING PARAMETERS:
  208.     name:
  209.         pointer to file name to transmit.
  210. ===========================================================================*/
  211. wctxpn(name)
  212. char *name;
  213. {
  214.     char *p;
  215.  
  216.         /*??????*/
  217.     totsecs = -1;
  218.  
  219. #ifdef STATLINE
  220.     pstat("Awaiting pathname NAK");
  221. #else
  222.     printf("Awaiting pathname NAK ");
  223. #endif
  224.     if ((firstch=readbyte(400))==TIMEOUT)
  225.         return ERROR;
  226.     if (firstch==WANTCRC)
  227.         Crcflg=TRUE;
  228.  
  229.         /* don't send drive specification */
  230.     if (p=index(':', name))
  231.         name = ++p;
  232.  
  233.         /* sector number 0 for pathname */
  234.     if (wcputsec(name, 0, SECSIZ)==ERROR)
  235.     {
  236.         printf("Can't send pathname %s\n", name);
  237.         return ERROR;
  238.     }
  239. #ifndef STATLINE
  240.     printf("\r                               \r");
  241. #endif
  242.     return OK;
  243. } /* wctxpn */
  244.  
  245.  
  246. /****************************************************************************
  247. FUNCTION:
  248.     Adapted from CMODEM13.C, written by    Jack M. Wierda and Roderick W. Hart
  249.  
  250. CALLING PARAMETERS:
  251. ===========================================================================*/
  252. wcrx(name)
  253. char *name;
  254. {
  255.     int sendchar, sectnum, sectcurr;
  256. #ifdef DEFBYTL
  257.     int cblklen;            /* bytes to dump this block */
  258. #endif
  259.  
  260.     if (openrx(name)==ERROR)
  261.         return ERROR;
  262.     firstsec=TRUE;
  263.     totsecs=sectnum=0;
  264.     sendchar=Crcflg?WANTCRC:NAK;
  265.  
  266.     for (;;)
  267.     {
  268. #ifdef STATLINE
  269.         if (!Quiet)
  270.             pstat("block %3d %2dk", totsecs, totsecs/8 );
  271. #else
  272.         if (!Quiet && !View)
  273.             printf("\rblock %3d %2dk ", totsecs, totsecs/8 );
  274. #endif
  275.         purgeline();
  276.         sectcurr=wcgetsec(Utility.ubuf, (sectnum&0177)?70:130,
  277.           sendchar);
  278.         if (sectcurr==(sectnum+1 &0xff))
  279.         {
  280.             sectnum++;
  281. /*
  282.  * if the compiler supports longs && the o/s blocks the
  283.  *  exact length of files then and only then use the file length
  284.  *  info (if transmitted).
  285.  */
  286. #ifdef DEFBYTL
  287. #ifdef BYTEFLENGTH
  288.             wcj = cblklen = Bytesleft>blklen ? blklen:Bytesleft;
  289. #else
  290.             wcj = cblklen = blklen;
  291. #endif
  292. #else
  293.             wcj = blklen;
  294. #endif
  295.             for (cp=Utility.ubuf; --wcj>=0; )
  296.                 if (fputc(*cp++, fout)==ERROR)
  297.                 {
  298.                     printf("\nDisk Full\n");
  299.                     return ERROR;
  300.                 }
  301. #ifndef XMODEM
  302.             if (View)
  303.             {
  304. #ifdef DEFBYTL
  305.                 wcj = cblklen;
  306. #else
  307.                 wcj = blklen;
  308. #endif
  309.                 for (cp=Utility.ubuf;--wcj>=0;)
  310.                     putchar(*cp++);
  311.             }
  312. #endif
  313. #ifdef DEFBYTL
  314.             if ((Bytesleft -= cblklen) < 0)
  315.                 Bytesleft = 0;
  316. #endif
  317.             totsecs += blklen/128;
  318.             sendchar=ACK;
  319.         }
  320.         else if (sectcurr==sectnum)
  321.         {
  322.             wcperr("received dup block\n");
  323.             sendchar=ACK;
  324.         }
  325.         else if (sectcurr==WCEOT)
  326.         {
  327.             sendbyte(ACK);
  328.             /* don't pad the file any more than it already is */
  329.             closerx(FALSE);
  330.             return OK;
  331.         }
  332.         else if (sectcurr==ERROR)
  333.             return ERROR;
  334.         else
  335.         {
  336.             printf(" Sync Error: got %d\n", sectcurr);
  337.             return ERROR;
  338.         }
  339.     }
  340. } /* wcrx */
  341.  
  342.  
  343. /****************************************************************************
  344. FUNCTION:
  345.     wcgetsec fetches a Ward Christensen type sector.
  346.     Returns sector number encountered or ERROR if valid sector not received,
  347.     or CAN CAN received    or WCEOT if eot sector.
  348.     time is timeout for first char, set to 4 seconds thereafter
  349.     *************** NO ACK IS SENT IF SECTOR IS RECEIVED OK **************
  350.        (Caller must do that when he is good and ready to get next sector)
  351.  
  352. CALLING PARAMETERS:
  353.     rxbuf:
  354.         pointer to buffer to place received data from modem
  355.     time:
  356.     sendchar:
  357. ===========================================================================*/
  358. wcgetsec(rxbuf, time, sendchar)
  359. char *rxbuf;
  360. int time;
  361. {
  362.     int sectcurr;
  363.  
  364.     for (Lastrx=errors=0; errors<RETRYMAX; ++errors, ++toterrs)
  365.     {
  366.             /*